﻿@{
    ViewBag.Title = "Danh sách giao dịch";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
    string PartnerName = string.Empty;
}
<script>
    app.controller('OrderController', function ($scope, $compile, $http, $filter, service) {

        $scope.loadinit = function(){
            var model = @Html.Raw(Json.Encode(Request.Url.AbsolutePath.Split('/')[Request.Url.AbsolutePath.Split('/').Length - 1]));
            if(isNaN(parseInt(model,10)))  $scope.partnerID = '';
            else  $scope.partnerID = parseInt(model);

            var url = '@url' + '@Url.Action("PartnerApi", "Api", new { id = Request.Url.AbsolutePath.Split('/')[Request.Url.AbsolutePath.Split('/').Length - 1] })';
            $http.get(url)
                .success(function (data) {  
                    $scope.partnerName = data.PartnerName;   
                })
                .error(function (data) {
                });            
        };

        $scope.load = function (page) {
            if(!$scope.firstload) $scope.partnerID = '';
            if (page === 0 || (page > $scope.pageCount && $scope.pageCount > 0))
                return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];

            var url = '@url' + '@Url.Action("OrderApi", "Api")';
            $http.get(url, {
                params: {
                    pageNumber: page,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    orderCode: $scope.orderCode,
                    partnerID: $scope.partnerID,
                    partnerName: $scope.partnerName,
                    status: $scope.status.value,
                    datefrom: $filter('date')(Date.parse($scope.datefrom), 'y/MM/dd'),
                    dateto: $filter('date')(Date.parse($scope.dateto), 'y/MM/dd')
                }
            })
                .success(function (data) {
                    service.setCommonData($scope, data, data.Orders);
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data.InnerException.InnerException);
                });
            $scope.firstload = false;
        };

        $scope.setSelected = function (id) {
            service.setSelected($scope, id);
        };

        $scope.check = function (id) {
            $scope.checkedObj[id] = !$scope.checkedObj[id];
        };

        $scope.setSelectedAll = function () {
            service.setSelectedAll($scope);
        };

        $scope.refresh = function (keyCode) {
            if (keyCode == undefined || keyCode === 13)
                $scope.load($scope.pageNumber);
        };

        $scope.clearrefresh = function () {
            $scope.orderCode = '';
            $scope.partnerName = '';
            $scope.status = { name: '', value: '' };
            $scope.datefrom = '';
            $scope.dateto = '';
            $scope.load($scope.pageNumber);
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "Order")';
        };

        $scope.edit = function (id) {
            if (id) {
                window.location = '@Url.Action("Detail", "Order")/' + id;
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.location = '@Url.Action("Detail", "Order")/' + selectedObj;
            }
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var deleteObj = service.getSelected($scope);
            if (deleteObj === '') return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("OrderApi", "Api")';
            $http.delete(url, {
                params: {
                    ids: deleteObj,
                    pageNumber: $scope.pageNumber,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    orderCode: $scope.orderCode,
                    partnerName: $scope.partnerName,
                    status:$scope.status.value,
                    datefrom: $filter('date')(Date.parse($scope.datefrom), 'y/MM/dd'),
                    dateto: $filter('date')(Date.parse($scope.dateto), 'y/MM/dd')
                }
            }).success(function (data) {
                service.setCommonData($scope, data, data.Orders);
                stoploading($('#content'));
                service.showModal($scope, $compile, 'notify', 'Đã xóa các đơn hàng có trạng thái Mới!!!');
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.sort = function (ob) {
            service.setSortingValue($scope, ob);
            $scope.load($scope.pageNumber);
        };

        $scope.statuslist = [{}];
        $scope.statuslist['N'] = 'Mới';
        $scope.statuslist['I'] = 'Đã lập kế hoạch';
        $scope.statuslist['C'] = 'Hoàn thành';

        $scope.statuss = [{}];
        $scope.statuss.length=0;
        $scope.statuss.push ({name:"Tất cả", value:''});
        $scope.statuss.push ({name:"Mới", value:'N'});
        $scope.statuss.push ({name:"Đã lập kế hoạch", value:'I'});
        $scope.statuss.push ({name:"Hoàn thành", value:'C'});
        $scope.status = {name:'',value:''};

        $scope.types = [{}];
        $scope.types['G'] = 'Giao';
        $scope.types['N'] = 'Nhận';

        $scope.orderBy = 'OrderID';
        $scope.orderDirection = 'descending';
        $scope.orderCode = '';
        $scope.partnerName = '';
        $scope.partnerID = '';
        $scope.firstload=true;
        $scope.checkedList = [];
        $scope.checkedObj = [{}];
        $scope.loadinit();
        $scope.load(1);
    });
</script>
<div ng-controller="OrderController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_ListViewHeader")

    <div id="content" class="content row">
        <div class="panel-body">
            <div id="search">
                <div class="row" style="margin-right: 0px">
                    <div class="col-lg-2">
                        <div class="form-group">
                            <label style="width: 110px">
                                Mã số giao dịch
                            </label>
                            <input ng-model="orderCode" ng-keypress="refresh($event.keyCode)" type="search" class="form-control" />
                        </div>
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_TextBox", new Dictionary<string, string>
                    {
                        {"Text", "Khách hàng"},
                        {"Model", "partnerName"},
                        {"Name", "partnerName"},
                    })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                    {
                        {"Text", "Tình trạng"},
                        {"Model", "status"},
                        {"Name", "status"},
                        {"Options", "status.name for status in statuss track by status.value"}
                    })
                    </div>
                    <div class="col-lg-2" style="clear: both">
                        @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "Từ ngày"},
                        {"Model", "datefrom"},
                        {"Name", "datefrom"}
                    })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "đến"},
                        {"Model", "dateto"},
                        {"Name", "dateto"}
                    })
                    </div>
                    @Html.Partial("_Search")
                </div>
            </div>
            <div class="table-responsive">
                @Html.Partial("_Toolbar_PagedList")
                <table id="data" class="table table-striped table-bordered table-hover">
                    <colgroup>
                        <col style="width: 50px" />
                        <col style="width: 150px" />
                        <col style="width: 150px" />
                        <col />
                        <col style="width: 150px" />
                        <col style="width: 150px" />
                        <col style="width: 150px" />
                    </colgroup>
                    <thead>
                        <tr>
                            <th>
                                <div>
                                    <input ng-model="selectedAll" ng-click="setSelectedAll()" style="margin-left: 10px;" class="center" type="checkbox">
                                </div>
                            </th>
                            <th ng-click="sort('OrderCode')">MS giao dịch
                                <div ng-class="orderBy != 'OrderCode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>                            
                            <th ng-click="sort('PartnerName')">Khách hàng
                                <div ng-class="orderBy != 'PartnerName' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Type')">Loại giao dịch
                                <div ng-class="orderBy != 'Type' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('OrderDate')" class="center">Ngày đặt hàng
                                <div ng-class="orderBy != 'OrderDate' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('DeliveryDate')" class="center">Ngày giao dịch
                                <div ng-class="orderBy != 'DeliveryDate' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')"
                                    class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Status')" class="center">Tình trạng
                                <div ng-class="orderBy != 'Status' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')"
                                    class="sorting fa">
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-hide="totalItemOnPage == 0" ng-repeat="item in items" ng-click="setSelected(item.OrderID)"
                            ng-dblclick="edit(item.OrderID)">
                            <td class="center">
                                <div>
                                    <input ng-click="check(item.OrderID)" ng-model="checkedObj[item.OrderID]" class="center" type="checkbox" />
                                </div>
                            </td>
                            <td>
                                @{
                                    var detailurl = Url.Action("Detail", "Order", new { id = "{{id=item.OrderID}}" });
                                    detailurl = HttpUtility.UrlDecode(detailurl);
                                }
                                <a data-ng-href="@detailurl">{{item.OrderCode}}</a>
                            </td>                            
                            <td>
                                @{
                                    var detaipartnerlurl = Url.Action("Detail", "Partner", new { id = "{{id=item.PartnerID}}" });
                                    detaipartnerlurl = HttpUtility.UrlDecode(detaipartnerlurl);
                                }
                                <a data-ng-href="@detaipartnerlurl">{{item.PartnerName}}</a>
                            </td>
                            <td>{{types[item.Type]}}</td>
                            <td class="center">{{item.OrderDate | date:'dd/MM/y'}}</td>
                            <td class="center">{{item.DeliveryDate | date:'dd/MM/y'}}</td>
                            <td>{{statuslist[item.Status]}}</td>
                        </tr>
                        <tr ng-show="totalItemOnPage == 0">
                            <td colspan="100">Không có dữ liệu
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
